package com.mifengs.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.AdvertBean;
import com.mifengs.mall.entity.BannerBean;
import com.mifengs.mall.entity.FloorBean;
import com.mifengs.mall.loader.GlideImageLoader;
import com.mifengs.mall.ui.web.CommonWebActivity;
import com.mifengs.mall.util.ImageLoadUtil;
import com.mifengs.mall.util.ToIntentUtil;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by lihaoqi on 2017/10/31.
 */

public class HomeOneFloorTypeAdapter extends  DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private FloorBean floorBean;
    public HomeOneFloorTypeAdapter(Context mContext) {
        this.mContext = mContext;
    }
    public void setFloorData(FloorBean floorBean){
        this.floorBean = floorBean;
        notifyDataSetChanged();
    }



    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return  new LinearLayoutHelper();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OneFloorViewHolder(LayoutInflater.from(mContext).inflate(R.layout.fragment_one_floor,parent,false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof OneFloorViewHolder){
            OneFloorViewHolder viewHolder = (OneFloorViewHolder)holder;
            final ImageView one_floor_one_img = viewHolder.one_floor_one_img;
            final ImageView one_floor_two_img = viewHolder.one_floor_two_img;
            final ImageView one_floor_three_img =viewHolder.one_floor_three_img;
            TextView one_floor_type_name = viewHolder.one_floor_type_name;
            Banner banner = viewHolder.banner;

            if(floorBean !=null){
                if(floorBean.getBanners().size()>0){
                    banner.setVisibility(View.VISIBLE);
                    List<BannerBean> banners = floorBean.getBanners();
                    banner.setImages(banners)
                            .setImageLoader(new GlideImageLoader())
                            .setDelayTime(5000)
                            .setOnBannerListener(new OnBannerListener() {
                                @Override
                                public void OnBannerClick(int position) {
                                    ToIntentUtil.toCommonIntent(floorBean.getBanners().get(position).getActionUrl(),mContext);
                                }
                            })
                            .start();
                }else{
                    banner.setVisibility(View.GONE);
                }
                one_floor_type_name.setText("--------  "+floorBean.getText()+"  --------");
                for(int i = 0;i<floorBean.getAdverts().size();i++ ){
                    AdvertBean adverts = floorBean.getAdverts().get(i);
                   switch (i){
                       case 0:
                           ImageLoadUtil.displayImage(mContext,adverts.getImg(),one_floor_one_img);
                           break;
                       case 1:
                           ImageLoadUtil.displayImage(mContext,adverts.getImg(),one_floor_two_img);
                           break;
                       case 2:
                           ImageLoadUtil.displayImage(mContext,adverts.getImg(),one_floor_three_img);
                           break;
                   }
                }
            }

            one_floor_one_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AdvertBean adverts = floorBean.getAdverts().get(0);
                    ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                }
            });
            one_floor_two_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AdvertBean adverts = floorBean.getAdverts().get(1);
                    ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                }
            });
            one_floor_three_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AdvertBean adverts = floorBean.getAdverts().get(2);
                    ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                }
            });

        }
    }



    @Override
    public int getItemCount() {
        return 1;
    }


    public class OneFloorViewHolder extends RecyclerView.ViewHolder{

        Banner banner;
        ImageView one_floor_one_img;
        ImageView one_floor_two_img;
        ImageView one_floor_three_img;
        TextView one_floor_type_name;
        public OneFloorViewHolder(View itemView) {
            super(itemView);
            banner = (Banner) itemView.findViewById(R.id.banner);
            one_floor_one_img = (ImageView)itemView.findViewById(R.id.one_floor_one_img);
            one_floor_two_img = (ImageView)itemView.findViewById(R.id.one_floor_two_img);
            one_floor_three_img = (ImageView)itemView.findViewById(R.id.one_floor_three_img);
        }
    }


}
